package com.example.mindadinner.Activity.myView;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.mindadinner.R;
import com.example.mindadinner.Activity.FoodDetailActivity;
import com.example.mindadinner.Activity.MiddleFragment;
import com.example.mindadinner.Activity.myView.pullToRefresh.PullToRefreshAdapterViewBase.OnScrollingLisner;
import com.example.mindadinner.Activity.myView.pullToRefresh.PullToRefreshBase.OnRefreshListener;
import com.example.mindadinner.Activity.myView.pullToRefresh.PullToRefreshListView;
import com.example.mindadinner.adapter.MultilateralListViewAdapter;
import com.example.mindadinner.adapter.PagerAapterForViews;
import com.example.mindadinner.util.Loger;
import com.haarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.viewpagerindicator.CirclePageIndicator;

public class MiddleFragementItemImpl1 {
	private Context con;
	private ListView listView;
	private LayoutInflater lf;
	private PullToRefreshListView mPullRefreshListView;
	private ImageView mTopImageView;

	public MiddleFragementItemImpl1(Context con) {
		this.con = con;
		lf = LayoutInflater.from(this.con);
	}

	public void setChileVeiwPagerAdapter(ChildViewPager ViewPager) {
		List<View> viewList = new ArrayList<View>();
		List<String> titleList = new ArrayList<String>();
		LayoutInflater lf = LayoutInflater.from(con);
		for (int i = 0; i < 2; i++) {

			View v = lf.inflate(R.layout.relativelayout, null);
			RelativeLayout rr = (RelativeLayout) v.findViewById(R.id.rl);
			if (i == 1) {
				rr.setBackgroundResource(R.drawable.meinv);
			} else {
				rr.setBackgroundResource(R.drawable.meinv);
			}
			viewList.add(v);
		}

		ViewPager.setAdapter(new PagerAapterForViews(viewList, titleList));
	}

	public void setListViewAdapter() {

		SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(
				new ViewPagerItemAdpater(2));
		swingBottomInAnimationAdapter.setListView(listView);
		listView.setAdapter(swingBottomInAnimationAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(((MiddleFragment) con),FoodDetailActivity.class);
				((MiddleFragment) con).startActivity(intent);
				((MiddleFragment) con).overridePendingTransition(
						R.anim.open_next, R.anim.close_main);
			}
		});
	}
	public void setPullRefreshListView() {
		mPullRefreshListView.setBackToTopView(mTopImageView);
		
		mPullRefreshListView.setOnScrollingLisner(new OnScrollingLisner() {

			@Override
			public void OnScrolling(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				Loger.V("huidiao");

				if(firstVisibleItem>20){
					mTopImageView.setVisibility(View.VISIBLE);
				}else if(mTopImageView.isShown()){
					mTopImageView.setVisibility(View.GONE);
				}
			}
		});
		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh(int currentMode) {
				if (currentMode == 1) {
					
				}
			}
		});
	}

	public View getMiddleViewViewPagerItemView() {
		LayoutInflater lf = LayoutInflater.from(con);
		View v = lf.inflate(R.layout.activity_middle_item, null);
		mPullRefreshListView = (PullToRefreshListView) v.findViewById(R.id.pull_refresh_list);
		mTopImageView = (ImageView) v.findViewById(R.id.lv_backtotop);
		setPullRefreshListView();
		listView = mPullRefreshListView.getRefreshableView();
		setListViewAdapter();
		return v;
	}
	
	private class ViewPagerItemAdpater extends MultilateralListViewAdapter {

		private ViewHolder viewHolder=new ViewHolder();

		protected ViewPagerItemAdpater(int types) {
			super(types);
		}

		@Override
		public int getCount() {
			return 30;
		}

		@Override
		public int getItemViewType(int position) {
			if (position == 0) {
				return 0;
			}
			return 1;
		}

		@Override
		public Object setViewHolder() {
			return viewHolder ;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public View switchDifTypes(View convertView, int type, Object holder) {
			viewHolder = ((ViewHolder) holder);
			switch (type) {
			case 0:
				convertView = lf.inflate(R.layout.food_viewpager, null);
				viewHolder.childViewPager = (ChildViewPager) convertView
						.findViewById(R.id.phototpager);
				viewHolder.circleIndicator = (CirclePageIndicator) convertView
						.findViewById(R.id.circleindicator);
				break;
			case 1:

				convertView = lf.inflate(R.layout.food_detial_list_item, null);
				viewHolder.textView = (TextView) convertView
						.findViewById(R.id.food_textview);
				viewHolder.imageView = (ImageView) convertView
						.findViewById(R.id.food_imageview);
				break;
			}
			return convertView;
		}

		@Override
		public void SetDifTypesView(int type) {
			if (viewHolder.textView != null) {

				viewHolder.textView
						.setText("多美美味的食物啊！！！！！！！！！！！！！！！！！！！！！！！！！！   ");

			} else {
				setChileVeiwPagerAdapter(viewHolder.childViewPager);
				viewHolder.circleIndicator
						.setViewPager(viewHolder.childViewPager);
			}
		}
	}

	private static class ViewHolder {
		public ImageView imageView;
		public TextView textView;
		public ChildViewPager childViewPager;
		public CirclePageIndicator circleIndicator;
	}

}
